<template>
	<view>
		<u-navbar :is-back="true" title="预约表" :border-bottom="false" :background="{backgroundColor: '#00a8ff'}">
			<view class="navbar-right" slot="right" @click="openinfo('/pages/order/makeTable/addArrange/addArrange')">
				<u-icon name="plus" color="#ffffff" slot: size="30" style="margin-right: 20rpx;"></u-icon>
			</view>
		</u-navbar>

		<!-- 背景 -->
		<view class="" style="background-color:#00a8ff;height: 100vh;">

			<!-- 日历部分 -->
			<view class="">
				<view class="u-p-t-26" style="text-align: center;color: #FFFFFF;font-weight: 700;">
					<u-icon name="arrow-left" @click="last()" class="u-m-r-26"></u-icon> {{str[0]}}年 {{str[1]}}月
					<u-icon name="arrow-right" @click="next()" class="u-m-l-26"></u-icon>
				</view>
				<view class="u-p-t-20 u-p-b-20">
					<view class="reservationTable u-m-b-20 u-m-t-20" style="width: 14%;display: inline-block;"
						v-for="(item,index) in weekText" :key="index">
						{{item}}
					</view>
					<view class="reservationTable u-m-b-20 u-m-t-20" style="width: 14%;display: inline-block;"
						v-for="(item,index) in dayday" @click="day(item)">
						<view class="" v-show="item!=' '">
							<p v-if="item==getDate&&getMouth==str[1]&&getDate==date&&getYear==str[0]">
								<text
									style="background-color: #FFFFFF;color: #0a7fff;border-radius: 50%;width: 40rpx;display: inline-block;">今</text>
							</p>
							<p v-else="item!=getDate">
								<text>{{item}}</text>
							</p>
											
						<!-- 	<p v-if="yue.'11'==[]" class="make">无预约</p>
							<p class="make" style="color: #0a7fff;">有预约</p> -->
							
						</view>
					</view>
				</view>
			</view>
			<!-- 透明白条 -->
			<view class="" style="margin: 0 52rpx;">
				<image src="../../../static/icon/tmbt.png" mode=""
					style="width: 100%;height: 25rpx;position: relative;top: 12rpx;"></image>
			</view>
			<!-- 预约详情 -->
			<view class="" style="background-color: #ffffff;margin: 0 26rpx;border-radius: 10rpx;">
				<view class="u-m-l-26 u-m-r-26 u-p-t-26 u-p-b-26">
					预约详情
				</view>

				<view class="u-flex u-m-l-26 u-m-r-26 u-p-t-26 u-p-b-26 li_line" v-show="makeShow==''">
					今日无预约订单
				</view>
				<view class="u-flex u-m-l-26 u-m-r-26 u-p-t-26 u-p-b-26 li_line"
					@click="openinfo('/pages/order/makeTable/details?id='+makeShow[0].id+'&name='+makeShow[0].userinfo.nickname)"
					v-for="(item,index) in makeShow" :key="index">
					<view class="u-flex-1" style="width: 100rpx;height: 100rpx;">
						<image :src="item.userinfo.avatar" style="width: 75rpx;height: 75rpx;border-radius: 50%;">
						</image>
					</view>
					<view class="u-flex-5 res">
						<p class="u-font-12">{{item.userinfo.nickname}}</p>
						<text style="color: #9e9e9e;font-size: 12rpx;" v-show="item.type=='1'">语音订单</text>
						<text style="color: #9e9e9e;font-size: 12rpx;" v-show="item.type=='2'">视频订单</text>
					</view>
					<view class="u-flex-1">
						<u-button size="mini" type="primary" shape="circle">
							{{$u.timeFormat(item.starttime, 'hh:MM')}}
						</u-button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				weekText: ["日", "一", "二", "三", "四", "五", "六"],
				// 预约表数据
				yue: [],
				dayday: [],

				// 预约信息

				makeShow: '',

				// 今日日期
				getDate: '',
				getMouth: '',
				getYear: '',
				date: '',
				month: '',
				year: '',
				time: '',
				str: ["", ""],
			}
		},
		onLoad(e) {
			console.log("预约表");
			console.log(e.timeTemp);
			// 拿到时间戳 拆分时间戳 分解年份 月份存放str
			this.time = this.$u.timeFormat(e.timeTemp, 'yyyy-mm');
			this.str = this.time.split('-');
			this.appointmentlist(e.timeTemp);
			// 获取今天
			let date = new Date();
			this.year = date.getFullYear();
			this.date = date.getDate();
			this.month = date.getMonth() + 1;
			// 
			console.log("今日多少号：" + this.date)

			setTimeout(() => {
				this.day(this.date);
			}, 3000)

		},
		methods: {
			openinfo(url) {
				uni.navigateTo({
					url: url,
				});
			},
			day(item) {
				item = /^\d$/.test(item) ? '0' + item : item;
				console.log(item);
				// console.log(this.yue[item][0].id);

				console.log("查找预约信息");
				this.makeShow = this.yue[item];


			},
			// 预约表
			appointmentlist(timeTemp) {
				this.$apiVipCentre.appointmentlist({
					datetime: timeTemp
				}).then((res) => {
					console.log("30天预约表: ", res.data.data);
					this.yue = res.data.data;
					console.log("this.yue:" + this.yue)
					this.dayday = Object.keys(this.yue);
					this.dayday = this.dayday.sort();
					// 判断处理当前月份第一天是星期几
					// 传入年份 月份 拿到星期几
					const weekDay = this.getWeek(this.str[0], this.str[1]);

					if (weekDay == '6') {
						// dayday里加上6个空客 过滤空格显示
						console.log("add")
						this.dayday.unshift(" ");
						this.dayday.unshift(" ");
						this.dayday.unshift(" ");
						this.dayday.unshift(" ");
						this.dayday.unshift(" ");
						this.dayday.unshift(" ");
					} else if (weekDay == '5') {
						this.dayday.unshift(" ");
						this.dayday.unshift(" ");
						this.dayday.unshift(" ");
						this.dayday.unshift(" ");
						this.dayday.unshift(" ");
					} else if (weekDay == '4') {
						this.dayday.unshift(" ");
						this.dayday.unshift(" ");
						this.dayday.unshift(" ");
						this.dayday.unshift(" ");
					} else if (weekDay == '3') {
						this.dayday.unshift(" ");
						this.dayday.unshift(" ");
						this.dayday.unshift(" ");
					} else if (weekDay == '2') {
						this.dayday.unshift(" ");
						this.dayday.unshift(" ");
					} else if (weekDay == '1') {
						this.dayday.unshift(" ");
					}
					console.log(this.dayday);
					const day = new Date();
					this.getDate = day.getDate();
					this.getYear = day.getFullYear();
					this.getMouth = day.getMonth() + 1;
					// console.log("this.yue: ",this.yue);
				}).catch((err) => {
					console.log('request fail', err);
				})
			},

			// 上一个月
			last() {
				console.log("上一个月");
				if (this.str[1] != "01") {
					console.log(this.str[1])
					this.str[1] = parseInt(this.str[1]) - 1;
					this.str[1] = /^\d$/.test(this.str[1]) ? '0' + this.str[1] : this.str[1];
				} else if (this.str[1] == "01") {
					// 年份减1 月份重置为12
					this.str[0] = parseInt(this.str[0]) - 1;
					this.str[1] = 12;
					this.str[1] = /^\d$/.test(this.str[1]) ? '0' + this.str[1] : this.str[1];
					console.log(this.str[1])
				}
				// 传入上个月时间戳
				let dt = this.str[0] + "-" + this.str[1] + "-" + "01" + " " + "00:00";
				var timestamp1 = (new Date(dt)).getTime() / 1000
				console.log("时间戳:", timestamp1);
				console.log("日期:", dt);

				this.appointmentlist(timestamp1)
				// 强制更新渲染
				this.$forceUpdate();
			},
			// 下一个月
			next() {
				// 先添加
				console.log("下一个月");
				if (this.str[1] != "12") {
					console.log(this.str[1])
					this.str[1] = parseInt(this.str[1]) + 1;
					this.str[1] = /^\d$/.test(this.str[1]) ? '0' + this.str[1] : this.str[1];
				} else if (this.str[1] == "12") {
					// 年份加1 月份重置为1
					this.str[0] = parseInt(this.str[0]) + 1;
					this.str[1] = 1;
					this.str[1] = /^\d$/.test(this.str[1]) ? '0' + this.str[1] : this.str[1];
					console.log(this.str[1])
				}
				// 传入下个月时间戳
				let dt = this.str[0] + "-" + this.str[1] + "-" + "01" + " " + "00:00";
				var timestamp1 = (new Date(dt)).getTime() / 1000
				console.log("时间戳:", timestamp1);
				console.log("日期:", dt);

				this.appointmentlist(timestamp1)
				// 强制更新渲染
				this.$forceUpdate();
			},
			open() {
				this.$refs.calendar.open();
			},
			confirm(e) {
				console.log(e);
			},

			//1.根据年度和月份，创建日期
			//应该先对year,month进行整数及范围校验的。
			getWeek(year, month) {

				var d = new Date();
				d.setYear(year);
				d.setMonth(month - 1);
				d.setDate(1);
				console.log(d);
				//获得周几
				var weeks = ['7', '1', '2', '3', '4', '5', '6'];
				console.log(weeks[d.getDay()]);
				return weeks[d.getDay()];
			}
		}
	}
</script>

<style scoped lang="scss">
	.res {
		position: relative;
		top: -10rpx;
	}

	.li_line {
		border-bottom: 1px #eaeaea solid;
	}

	.reservationTable {
		font-size: 28rpx;
		color: #f1faff;
		text-align: center;
	}

	.make {
		font-size: 16rpx;
		text-align: center;
	}
</style>
